package com.geofitti.andoird;



import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditProfileActivity extends Activity {
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.geofitti_edit_profile);	
		
		final EditText editEmail = (EditText) findViewById(R.id.edit_email);
		final EditText editPassword = (EditText) findViewById(R.id.edit_password);
		final EditText editFav = (EditText) findViewById(R.id.edit_favorites);
		final EditText editLoc = (EditText) findViewById(R.id.edit_location);
	    final Button viewEditProfile = (Button) findViewById(R.id.btnEditPro);
	    
		try {
			String paramString = "?user_id=12";
			JSONObject jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/getProfile" + paramString);
			final String email 	  =	jsonO.getString("email");
			final String password =	jsonO.getString("password");
			final String favorite =	jsonO.getString("favorite");
			final String location = jsonO.getString("location");
			editEmail.setText(email);
			editPassword.setText(password);
			editFav.setText(favorite);
			editLoc.setText(location);
    	} catch (JSONException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
		}
		
		viewEditProfile.setOnClickListener(new View.OnClickListener(){
	    	public void onClick(View arg0){	
	    		try {
	    			String paramString = "?email=" + editEmail.getText() + "&password=" + editPassword.getText() + "&favorite=" + editFav.getText() + "&location=" + editLoc.getText() + "&user_id=12";
	    			JSONObject jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/updateUser" + paramString);
	    			//JSONObject jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/updateUser?location=saltlakecity&user_id=12");
	    			final String status = jsonO.getString("status");
					AlertDialog.Builder builder = new AlertDialog.Builder(EditProfileActivity.this);
					builder.setMessage(jsonO.getString("message"))
					       .setCancelable(false)
					       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
									if (status.equals("ok")){
										Intent i = new Intent(getApplicationContext(), MenuActivity.class);
								       	startActivity(i);
								       	EditProfileActivity.this.finish(); 
								   }
								   else{ //account wasn't registered
									 	
									   dialog.dismiss();
								   }
								
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
	    		} catch (JSONException e) {
	    			// TODO Auto-generated catch block 
	    			e.printStackTrace();
	    		}
	    	}
		});
	}								
}
